package com.chc.web.reader.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chc.utils.CommonResult;
import com.chc.web.bookcase.entity.BookCase;
import com.chc.web.reader.entity.Reader;
import com.chc.web.reader.entity.ReaderParm;
import com.chc.web.reader.service.ReaderService;
import com.chc.web.reader_role.entity.ReaderRole;
import com.chc.web.reader_role.service.ReaderRoleService;
import com.chc.web.user.entity.User;
import com.chc.web.user.entity.UserParm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/04/21/22:05
 * @Description:
 */

@RestController
@RequestMapping("/api/reader")
public class ReaderController {

    @Autowired
    private ReaderService readerService;

    @Autowired
    private ReaderRoleService readerRoleService;

    //根据学号查询出读者信息
    @GetMapping("/getByIdReader")
    public CommonResult ReaderList(String cardid){
        QueryWrapper<Reader> query = new QueryWrapper<>();
        query.lambda().eq(Reader::getCardid,cardid);
        Reader one = readerService.getOne(query);
        return CommonResult.success("查询成功",one);
    }

    //查询读者列表
    @GetMapping("/list")
    public CommonResult list(ReaderParm parm){
        IPage<Reader> list = readerService.list(parm);
        return CommonResult.success("查询成功",list);
    }

    /**
     * 新增图书
     * @param reader
     * @return
     */
    @PostMapping
    public CommonResult addReader(@RequestBody Reader reader){
        //查询用户名是否被占用
        QueryWrapper<Reader> query = new QueryWrapper();
        query.lambda().eq(Reader::getUsername, reader.getUsername());
        Reader one = readerService.getOne(query);
        if (one != null) {
            return CommonResult.error("帐户已被占用!");
        }
        readerService.saveReader(reader);
        return CommonResult.success("新增读者成功");
    }

    /**
     * 编辑图书
     * @param reader
     * @return
     */
    @PutMapping
    public CommonResult editReader(@RequestBody Reader reader){
        //保存修改后的读者
        QueryWrapper<ReaderRole> query = new QueryWrapper<ReaderRole>();
        query.lambda().eq(ReaderRole::getUserId,reader.getId());
        ReaderRole readerRole = new ReaderRole();

        readerRole.setRoleId(reader.getRoleId());
        readerRoleService.update(readerRole,query);

//        readerRole.setUserId(reader.getId());
//        readerRoleService.updateById(readerRole);
        boolean save = readerService.updateById(reader);
        if(save){
            return CommonResult.success("修改读者成功");
        }
        return CommonResult.error("修改读者失败");
    }

    /**
     * 删除角色
     * @param bookId
     * @return
     */
    @DeleteMapping("/{id}")
    public CommonResult deleteReader(@PathVariable("id") Long bookId){
        boolean save = readerService.removeById(bookId);
        if(save){
            return CommonResult.success("删除读者成功");
        }
        return CommonResult.error("删除读者失败");
    }
}
